package com.yeyks.service.impl;

import com.yeyks.dal.dao.AppInfoMapper;
import com.yeyks.dal.domain.AppInfo;
import com.yeyks.dto.AppInfoDTO;
import com.yeyks.model.enums.AppEnum;
import com.yeyks.service.AppInfoService;
import com.yeyks.util.ObjectConvertUtil;
import com.yeyks.dto.AppInfoDTO;
import com.yeyks.model.enums.AppEnum;
import com.yeyks.service.AppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author fuchangshuai
 * @date 2019/4/3 9:49 AM
 */
@Slf4j
@Service
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoMapper appInfoMapper;


    /**
     * 根据类型和版本信息获取更新信息
     *
     * @param type
     * @param version
     * @return
     */
    @Override
    public AppInfoDTO getAppInfoByType(String type, String version) {
        AppInfoDTO appInfoDTO = null;
        try {
            AppInfo appInfo = appInfoMapper.selectByType(type);
            if (appInfo == null) {
                log.info("通过" + type + "类型的没找到合适版本信息");
                return null;
            }
            appInfoDTO = ObjectConvertUtil.convertObject(appInfo, AppInfoDTO.class);
            //安卓
            if (AppEnum.Android.desc().equals(type)) {
                int androidVersion = appInfo.getVersionCode();
                int nowVersion = Integer.valueOf(version);
                if (nowVersion >= androidVersion) {
                    appInfoDTO.setForcedUpdate(0);
                } else {
                    appInfoDTO.setTitle("立即更新");
                }
            }
            //IOS
            if (AppEnum.iOS.desc().equals(type)) {
                String versionName = appInfo.getVersion();
                versionName = versionName.replaceAll("\\.", "");
                version = version.replaceAll("\\.", "");
                int iosVersion = Integer.valueOf(versionName);
                int nowVersion = Integer.valueOf(version);
                if (nowVersion >= iosVersion) {
                    appInfoDTO.setForcedUpdate(0);
                } else {
                    appInfoDTO.setTitle("立即更新");
                }
            }
        } catch (Exception e) {
            log.info("获取版本信息失败{}", e.getLocalizedMessage());
            return appInfoDTO;
        }
        return appInfoDTO;
    }
}
